<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="/admin/include :: head">
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<section class="ftco-section">
    <div id="vueController" style="margin-left: 20px">
        <h2>新增产品</h2>
        <el-divider></el-divider>
        <el-form :model="form">
            <el-form-item label="产品名称">
                <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="产品名称标题">
                <el-input v-model="form.title" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="产品类型">
                <el-select v-model="form.productType" placeholder="请选择产品类型">
                    <el-option
                            v-for="item in productTypeOptions"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="顺序">
                <el-input v-model="form.order" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="产品主图">
                <el-upload
                        action="/common/upload"
                        ref="upload"
                        :limit="1"
                        :file-list="fileList"
                        :on-success="handleUploadFinish"
                        list-type="picture">
                    <!--                    <i class="el-icon-upload"></i>-->
                    <el-button size="small" type="primary">点击上传</el-button>
                    <!--                    <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>-->
                </el-upload>
            </el-form-item>
            <el-form-item label="产品详情文章">

            </el-form-item>
            <el-row :gutter="10">
                <el-col :md="15" >
                    <div ref="editor" class="text" id="editor"></div>
                </el-col>
                <el-col :md="9" >
                    <div v-html="form.content"></div>
                </el-col>
            </el-row>
        </el-form>

    </div>
</section>

</body>
<script type="module">
    import {getProductTypeList, getProductList, saveProduct, delProduct} from "/static/js/api/productApi.js";
    // import Base64UploadAdapter from '@ckeditor/ckeditor5-upload/src/adapters/base64uploadadapter';
    var E = window.wangEditor
    new Vue({
        el: '#vueController',
        data() {
            return {
                dataList: [],
                fileList: [],
                dialogFormVisible: false,
                form: {},
                edit: false,
                productTypeOptions: [],
                formTemplate: {
                    id: "",
                    order: undefined,
                    imgUrl: "",
                    productType: undefined,
                    content: "开始吧",
                    name: "",
                },
            }
        },
        created() {
            this.getList();
            getProductTypeList().then(res => {
                this.productTypeOptions = res.data;
            })
        },
        methods: {
            getList() {
                getProductList().then(res => {
                    console.log(res)
                    this.dataList = res.data;
                })
            },
            getProductTypeNameById(v) {
                let name = "未知产品类型"
                this.productTypeOptions.forEach(item => {
                    if (item.id === v.productType) {
                        name = item.name
                    }
                })
                return name
            },
            handleUploadFinish(response, file, fileList) {
                console.log(response)
                this.form.imgUrl = response.data;
            },
            handleDelete(index, row) {
                delProduct(row.id).then(res => {
                    console.log("删除成功");
                    this.getList();
                })
            },
            handleSubmit() {
                saveProduct(this.form).then(res => {
                    console.log("添加成功");
                    this.getList();
                })
                this.fileList = [];
                this.dialogFormVisible = false;

            },
            handleAddDialog() {
                this.form = Object.assign({}, this.formTemplate);
                this.edit = false;
                this.dialogFormVisible = true;
            },
            handleEditDialog(index, row) {
                this.form = Object.assign({}, row);
                this.edit = true;
                this.dialogFormVisible = true;
            },
        },
        mounted() {
            const editor = new E(this.$refs.editor);
            // 自定义菜单配置
            // 设置编辑区域高度为 500px
            editor.config.height = 800
            editor.config.zIndex = 10
            editor.config.uploadImgShowBase64 = true
            editor.config.onchange =  (html)=> {
                // 第二步，监控变化，同步更新到 textarea
                console.log('change 之后最新的 html', html)
                this.form.content = html
            }
            editor.create()
            // 初始化数据
            editor.txt.html(this.form.content)


            // ClassicEditor
            //     .create( document.querySelector( '#editor' ), {
            //         plugins: [ Base64UploadAdapter],
            //     } )
        }

    })
</script>
<style>
    /* table 样式 */
    table {
        border-top: 1px solid #ccc;
        border-left: 1px solid #ccc;
    }
    table td,
    table th {
        border-bottom: 1px solid #ccc;
        border-right: 1px solid #ccc;
        padding: 3px 5px;
    }
    table th {
        border-bottom: 2px solid #ccc;
        text-align: center;
    }

    /* blockquote 样式 */
    blockquote {
        display: block;
        border-left: 8px solid #d0e5f2;
        padding: 5px 10px;
        margin: 10px 0;
        line-height: 1.4;
        font-size: 100%;
        background-color: #f1f1f1;
    }

    /* code 样式 */
    code {
        display: inline-block;
        *display: inline;
        *zoom: 1;
        background-color: #f1f1f1;
        border-radius: 3px;
        padding: 3px 5px;
        margin: 0 3px;
    }
    pre code {
        display: block;
    }

    /* ul ol 样式 */
    ul, ol {
        margin: 10px 0 10px 20px;
    }
</style>
</html>
